#include"list.h"

Login_list::Login_list():head(NULL),end(NULL),size(0)
{
	if(head==NULL)
		head=new Node;
	head->next=NULL;
	end=head;
}

void Login_list::insert(bufferevent *bfev,string &act,string &name)
{
	Node *node=new Node;
	Login_info lf(bfev,act,name);
	node->lf=lf;
	node->next=NULL;
	end->next=node;
	end=node;
	++size;
}

void Login_list::erase(bufferevent* bfev)
{
	Node *p=head;
	while(p->next!=NULL)
	{
		Node *pp=p->next;
		if((pp->lf).get_bfev()==bfev)
		{
			if(pp==end)
				end=p;
			Node *pa=pp->next;
			delete pp;
			p->next=pa;
			--size;
			break;
		}
		p=p->next;
	}
}

const string* Login_list::get_act(const bufferevent* bfev)
{
	Node *p=head->next;
	while(p!=NULL)
	{
		if((p->lf).get_bfev()==bfev)
		{
			return &((p->lf).get_act());
		}
		p=p->next;
	}
	return NULL;
}

const string* Login_list::get_name(const bufferevent* bfev)
{
	Node *p=head->next;
	while(p!=NULL)
	{
		if((p->lf).get_bfev()==bfev)
		{
			return &((p->lf).get_name());
		}
		p=p->next;
	}
	return NULL;
}

void Login_list::sd_meg(const string&str)
{
	Node *p=head->next;
	while(p!=NULL)
	{
		bufferevent_write(const_cast<bufferevent*>(p->lf.get_bfev()),str.c_str(),MAX_SBUF_SIZE);
		p=p->next;
	}
}

Login_list::~Login_list()
{
	while(size>0)
	{
		Node *p=head->next->next;
		delete head->next;
		head->next=p;
		--size;
	}
	delete head;
}

Login_info::Login_info():bfev(NULL),act(),name() {}

Login_info::Login_info(bufferevent *bfev,string &act,string &name):bfev(bfev),act(act),name(name) {}

Login_info::Login_info(const Login_info&lf)
{
	this->bfev=lf.bfev;
	act=lf.act;
	name=lf.name;
}

Login_info& Login_info::operator=(const Login_info&lf)
{
	if(this!=&lf)
	{
		this->bfev=lf.bfev;
		act=lf.act;
		name=lf.name;
	}
	return *this;
}

const bufferevent *Login_info::get_bfev()
{
	return bfev;
}

const string& Login_info::get_act()
{
	return act;
}

const string& Login_info::get_name()
{
	return name;
}
